package com.github.nuggets.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.nuggets.constant.DataBaseConstants;
import com.github.nuggets.model.entity.NuggetsAccountEntity;
import com.github.nuggets.model.entity.NuggetsFoucEntity;
import com.github.nuggets.model.result.PageResult;
import com.github.nuggets.repository.FoucRepository;
import com.github.nuggets.repository.db.NuggetsFoucMapper;
import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: wangsj
 * @date: 2022/3/17 6:40 下午
 */
@Service
public class FoucRepositoryImpl extends ServiceImpl<NuggetsFoucMapper, NuggetsFoucEntity> implements FoucRepository {


    @Override
    public Boolean addFoucRecord(Long account, Long foucAccount) {
        NuggetsFoucEntity foucEntity = new NuggetsFoucEntity();

        foucEntity.setAccountId(account);
        foucEntity.setFoucId(foucAccount);

        return this.save(foucEntity);
    }

    @Override
    public Optional<NuggetsFoucEntity> findFoucRecord(Long account, Long foucAccount) {

        NuggetsFoucEntity foucRecord = this.getOne(new LambdaQueryWrapper<NuggetsFoucEntity>()
                .eq(NuggetsFoucEntity::getAccountId, account)
                .eq(NuggetsFoucEntity::getFoucId, foucAccount)
        );
        return Optional.ofNullable(foucRecord);
    }

    @Override
    public List<Long> listFoucIdByAccount(Long accountId) {
        return this.list(
                new LambdaQueryWrapper<NuggetsFoucEntity>()
                        .select(NuggetsFoucEntity::getFoucId)
                        .eq(NuggetsFoucEntity::getIsCancel, DataBaseConstants.LOGIC_NOT_DELETE_VALUE)
                        .eq(NuggetsFoucEntity::getAccountId, accountId)

        ).stream().map(NuggetsFoucEntity::getFoucId).collect(Collectors.toList());
    }

    @Override
    public PageResult<Long> pageFoucIds(Long accountId, Integer pageIndex, Integer pageSize) {
        IPage<NuggetsFoucEntity> foucEntityIPage = page(
                new Page<>(pageIndex, pageSize),
                new LambdaQueryWrapper<NuggetsFoucEntity>()
                        .select(NuggetsFoucEntity::getFoucId)
                        .eq(NuggetsFoucEntity::getIsCancel, DataBaseConstants.LOGIC_NOT_DELETE_VALUE)
                        .eq(NuggetsFoucEntity::getAccountId, accountId)
        );

        List<Long> ids = foucEntityIPage
                .getRecords()
                .stream()
                .map(NuggetsFoucEntity::getFoucId)
                .collect(Collectors.toList());
        return PageResult.of(foucEntityIPage,ids);
    }


}
